{% assign blockId = block_id | default: section.block_id %}
{% assign data = section.settings %}

{%- capture fill_container -%}
  {%- if data.is_fill -%}
    full_container_wrapper
  {%- else -%}
    container_wrapper
  {%- endif -%}
{%- endcapture -%}

<div class="block_collection">
  <div class="{{fill_container}}">
    {% include 'swiper_title',title: data.title,detail: data.detail,blockId: blockId %}
    <div class="swiper overflow-hidden" id="swiper-{{blockId}}">
      <ul class="swiper-wrapper">
        {% for block in section.blocks %}
          {% get_collection collections_id={block.collection.id} collection_image=1 %}
          {%- capture handle -%}
            {% if collection.handle %}/collections/{{ collection.handle }}?data_from={{data_from}}
              {% else %}javascript:;{% endif %}
          {%- endcapture -%}
          <li class="swiper-slide">
            <a href="{{handle}}">
              <div class="img_box">
                {% include 'lazy_img',src: collection.src %}
              </div>
              <div class="item_title mt-6 text-center public_title">{{collection.title | default: 'Collection title'}}</div>
            </a>
          </li>
        {% endfor %}
      </ul>
    </div>
    {% if section.settings.more_text != ""%}
      <div class="more_wrapper flex justify-center">
        <a class="secondary_btn" href="/collections/">{{ section.settings.more_text }}</a>
      </div>
    {% endif %}
  </div>
</div>


<script>
  (function() {
    let blocksSize = '{{section.blocks.size}}';
    let options = {}

    if ((blocksSize > Number(`{{data.pc_number}}`) && $(window).width() >= 768) || (blocksSize > Number(`{{data.yd_number}}`) && $(window).width() < 768)) {
      options.navigation = {
        nextEl: `#swiper-button-next-{{blockId}}`,
        prevEl: `#swiper-button-prev-{{blockId}}`
      }
      $(`#swiper_title_wrapper-{{blockId}} .right`).css('display', 'flex')
    }

    let swiper = new Swiper(`#swiper-{{blockId}}`, {
      slidesPerView: $(window).width() >= 768
        ? '{{data.pc_number}}'
        : '{{data.yd_number}}',
      spaceBetween: 20,
      ... options
    })
  })()
</script>

{% schema %}
  {
    "tag": "section",
    "class": "block_collection",
    "is_global": false,
    "name": {
      "zh_CN": "专辑列表",
      "en_US": "Collection list"
    },
    "max_blocks": "20",
    "settings": [
      {
        "type": "card_header",
        "label": {
          "zh_CN": "设置",
          "en_US": "Settings"
        }
      },
      {
        "type": "card_input",
        "id": "title",
        "label": {
          "zh_CN": "标题",
          "en_US": "Title"
        },
        "placeholder": {
          "zh_CN": "请输入标题",
          "en_US": "Please enter title"
        },
        "default": "Collection List"
      },
      {
        "type": "card_text_editor",
        "id": "detail",
        "label": {
          "zh_CN": "简短描述",
          "en_US": "Short description"
        },
        "placeholder": {
          "zh_CN": "请输入描述",
          "en_US": "Please enter a description"
        },
        "default": "Collection Detail"
      },
      {
        "type": "card_switch",
        "label": {
          "zh_CN": "宽度铺满",
          "en_US": "Spread the width"
        },
        "id": "is_fill",
        "default": "false"
      }, {
        "type": "card_slider",
        "id": "pc_number",
        "max": "6",
        "min": "3",
        "label": {
          "zh_CN": "PC每排数量",
          "en_US": "PC number per row"
        },
        "default": "3"
      }, {
        "type": "card_slider",
        "id": "yd_number",
        "max": "2",
        "min": "1",
        "label": {
          "zh_CN": "移动端每排数量",
          "en_US": "Number of mobile terminals per row"
        },
        "default": "2"
      }, {
        "type": "card_input",
        "id": "more_text",
        "label": {
          "zh_CN": "查看更多文案",
          "en_US": "Text of view more"
        }
      }
    ],
    "blocks": [
      {
        "name": {
          "zh_CN": "专辑列表",
          "en_US": "Collection list"
        },
        "type": "item",
        "settings": [
          {
            "type": "card_collection",
            "label": {
              "zh_CN": "选择专辑",
              "en_US": "Select collection"
            },
            "default": {
              "id": "",
              "title": ""
            },
            "id": "collection"
          }
        ]
      }
    ],
    "default": {
      "settings": {
        "title": "Shop by collection",
        "detail": "Share information about your brand with your customers.",
        "pc_number": "4",
        "yd_number": "2",
        "more_text": "View all"
      },
      "blocks": [
        {
          "collection": {
            "id": "",
            "title": ""
          },
          "block_type": "item"
        }, {
          "collection": {
            "id": "",
            "title": ""
          },
          "block_type": "item"
        }, {
          "collection": {
            "id": "",
            "title": ""
          },
          "block_type": "item"
        }, {
          "collection": {
            "id": "",
            "title": ""
          },
          "block_type": "item"
        }
      ]
    },
    "icon": "icon-shangpinpingpu"
  }
{% endschema %}